// DlgSysSetting.cpp : implementation file
//

#include "stdafx.h"
#include "CStudyChinese.h"
#include "DlgSysSetting.h"
#include "AppSetting.h"
#include "FaceManager.h"
#include "FaceHandler.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CDlgSysSetting dialog


CDlgSysSetting::CDlgSysSetting(CWnd* pParent /*=NULL*/)
	: CTabDlg(CDlgSysSetting::IDD, pParent)
{
	//{{AFX_DATA_INIT(CDlgSysSetting)
	m_bLoadCEDic = FALSE;
	m_bLoadECDic = FALSE;
	//}}AFX_DATA_INIT
}


void CDlgSysSetting::DoDataExchange(CDataExchange* pDX)
{
	CDialog::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CDlgSysSetting)
	DDX_Control(pDX, IDC_BTN_RESTORE, m_btnRestore);
	DDX_Control(pDX, IDC_HK_SOUND, m_hkSound);
	DDX_Control(pDX, IDC_HK_SHOWSETTING, m_hkShowSetting);
	DDX_Control(pDX, IDC_HK_MAINWND, m_hkMainWnd);
	DDX_Control(pDX, IDC_HK_GETWORD, m_hkGetWord);
	DDX_Check(pDX, IDC_CHECK1, m_bLoadCEDic);
	DDX_Check(pDX, IDC_CHECK2, m_bLoadECDic);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CDlgSysSetting, CTabDlg)
	//{{AFX_MSG_MAP(CDlgSysSetting)
	ON_BN_CLICKED(IDC_BTN_RESTORE, OnRestore)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CDlgSysSetting message handlers

void CDlgSysSetting::SaveUpdateSetting()
{
	CHotKeyInfo hk ;
	
	m_hkGetWord.GetHotKey( hk.m_wVirtualKeyCode ,hk.m_wModifiers ) ;
	if(! CAppSetting::Instance()->m_hkGetWord.IsSame(hk) )
	{
		CAppSetting::Instance()->m_hkGetWord.UnregisterHotKey(AfxGetMainWnd()->GetSafeHwnd(),HK_GETWORD);

		CAppSetting::Instance()->m_hkGetWord.UpdateHotKey(hk);

		CAppSetting::Instance()->m_hkGetWord.RegisterSelf(AfxGetMainWnd()->GetSafeHwnd(),HK_GETWORD);
	}

	m_hkMainWnd.GetHotKey( hk.m_wVirtualKeyCode ,hk.m_wModifiers ) ;
	if(! CAppSetting::Instance()->m_hkGetWord.IsSame(hk) )
	{
		CAppSetting::Instance()->m_hkMainWnd.UnregisterHotKey(AfxGetMainWnd()->GetSafeHwnd(),HK_MAINWND);

		CAppSetting::Instance()->m_hkMainWnd.UpdateHotKey(hk);

		CAppSetting::Instance()->m_hkMainWnd.RegisterSelf(AfxGetMainWnd()->GetSafeHwnd(),HK_MAINWND);
	}

	m_hkShowSetting.GetHotKey( hk.m_wVirtualKeyCode ,hk.m_wModifiers ) ;
	if(! CAppSetting::Instance()->m_hkShowSetting.IsSame(hk) )
	{
		CAppSetting::Instance()->m_hkShowSetting.UnregisterHotKey(AfxGetMainWnd()->GetSafeHwnd(),HK_SHOWSETTING);

		CAppSetting::Instance()->m_hkShowSetting.UpdateHotKey(hk);

		CAppSetting::Instance()->m_hkShowSetting.RegisterSelf(AfxGetMainWnd()->GetSafeHwnd(),HK_SHOWSETTING);
	}

	m_hkSound.GetHotKey( hk.m_wVirtualKeyCode ,hk.m_wModifiers ) ;
	if(! CAppSetting::Instance()->m_hkSound.IsSame(hk) )
	{
		CAppSetting::Instance()->m_hkSound.UnregisterHotKey(AfxGetMainWnd()->GetSafeHwnd(),HK_SOUND);

		CAppSetting::Instance()->m_hkSound.UpdateHotKey(hk);

		CAppSetting::Instance()->m_hkSound.RegisterSelf(AfxGetMainWnd()->GetSafeHwnd(),HK_SOUND);
	}
	UpdateData();
	CAppSetting::Instance()->m_bLoadCEDic = this->m_bLoadCEDic;
	CAppSetting::Instance()->m_bLoadECDic = this->m_bLoadECDic;
}

BOOL CDlgSysSetting::OnInitDialog() 
{
	CTabDlg::OnInitDialog();
	m_bLoadCEDic = CAppSetting::Instance()->m_bLoadCEDic;
	m_bLoadECDic = CAppSetting::Instance()->m_bLoadECDic;

	CFaceManager* p = CFaceManager::Instance();
	m_btnRestore.SetFlat(FALSE);
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_OUT,p->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_IN,p->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,p->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_OUT,p->m_TextColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_IN,p->m_TextColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		break;
	}
	m_hkGetWord.SetHotKey(CAppSetting::Instance()->m_hkGetWord.m_wVirtualKeyCode,CAppSetting::Instance()->m_hkGetWord.m_wModifiers) ;
	m_hkMainWnd.SetHotKey(CAppSetting::Instance()->m_hkMainWnd.m_wVirtualKeyCode,CAppSetting::Instance()->m_hkMainWnd.m_wModifiers) ;
	m_hkShowSetting.SetHotKey(CAppSetting::Instance()->m_hkShowSetting.m_wVirtualKeyCode,CAppSetting::Instance()->m_hkShowSetting.m_wModifiers) ;
	m_hkSound.SetHotKey(CAppSetting::Instance()->m_hkSound.m_wVirtualKeyCode,CAppSetting::Instance()->m_hkSound.m_wModifiers) ;
	UpdateData(FALSE);
	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

void CDlgSysSetting::OnRestore() 
{
	m_hkGetWord.SetHotKey(81,2) ;
	m_hkMainWnd.SetHotKey(87,2) ;
	m_hkShowSetting.SetHotKey(122,0) ;
	m_hkSound.SetHotKey(69,2) ;

	UpdateData(FALSE);
}

void CDlgSysSetting::UpdateFace()
{
	m_btnRestore.SetFlat(FALSE);
	switch(CAppSetting::Instance()->m_FaceMode)
	{
	case CAppSetting::FACE_NORMAL:
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceManager::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceManager::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceManager::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceManager::Instance()->m_TextColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceManager::Instance()->m_TextColor);
		break;
	case CAppSetting::FACE_NONRECTANGLE:
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_OUT,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_IN,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_BK_FOCUS,CFaceHandler::Instance()->m_bkColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_OUT,CFaceHandler::Instance()->m_TextColor);
		m_btnRestore.SetColor(CButtonST::BTNST_COLOR_FG_IN,CFaceHandler::Instance()->m_TextColor);
		break;
	}
}
